<?php

namespace app\common\traits;

trait StringPart
{
    public function getStringArrByExplode($attribute, string $delimiter = ',')
    {
        $content = $this[$attribute] ?? '';
        $arr = [];
        if ($content) {
            $arr = $this->getStringArrByValue($content, $delimiter);
        }
        return $arr;
    }

    public function getStringArrByValue(string $content, string $delimiter = ',')
    {
        return array_filter(explode($delimiter, $content));
    }

    public function getStringArrByChar($attribute)
    {
        $content = $this[$attribute];
        $words = [];
        if ($content) {
            for ($i = 0; $i < mb_strlen($content); $i++) {
                $words[] = mb_substr($content, $i, 1);
            }
        }
        return $words;
    }

    public function getStringCharArrByValue($content)
    {
        $words = [];
        if ($content) {
            for ($i = 0; $i < mb_strlen($content); $i++) {
                $words[] = mb_substr($content, $i, 1);
            }
        }
        return $words;
    }
}